# Generated by Django 2.2.9 on 2020-03-04 08:39

from django.contrib.auth import get_user_model
from django.db import migrations
from rolepermissions.checkers import has_role
from rolepermissions.permissions import revoke_permission
from rolepermissions.roles import assign_role


def reset_power_roles(apps, schema_editor):
    User = get_user_model()
    for user in User.objects.all():
        if has_role(user, "power_user"):
            # Re-assigning the role will sync the default associated permissions
            assign_role(user, "power_user")


def revoke_user_edit_cresus_employee_number_from_power_users(apps, schema_editor):
    User = get_user_model()
    for user in User.objects.all():
        if has_role(user, "power_user"):
            revoke_permission(user, "user_edit_cresus_employee_number")
            user.save()


class Migration(migrations.Migration):
    dependencies = [
        ("user", "0051_userprofile_cresus_employee_number"),
    ]

    operations = [
        migrations.RunPython(
            reset_power_roles, revoke_user_edit_cresus_employee_number_from_power_users
        ),
    ]
